package com.chtw.oasystem.controller;

import com.chtw.oasystem.bean.FullUserInfo;
import com.chtw.oasystem.bean.Users;
import com.chtw.oasystem.common.response.RespUtils;
import com.chtw.oasystem.respBean.CheckListResp;
import com.chtw.oasystem.respBean.ListUserResp;
import com.chtw.oasystem.respBean.LoginUser;
import com.chtw.oasystem.respBean.UserResp;
import com.chtw.oasystem.service.impl.UsersServiceImpl;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@ResponseBody
@RequestMapping("/user")
public class UsersController {

    private static final Logger logger = LoggerFactory.getLogger(UsersController.class);

    @Autowired
    private UsersServiceImpl usersService;

    @PostMapping("/create")
    public RespUtils<Boolean> createUser(HttpSession session, @RequestBody Users user) {
        try {
            boolean created = usersService.createUser(session, user);
            if (created) {
                return RespUtils.success(true);
            }
            return RespUtils.fail("添加用户失败！");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @PostMapping("/edit")
    public RespUtils<Boolean> editUser(HttpSession session, @RequestBody Users user) {
        try {
            boolean edit = usersService.editUser(session, user);
            if (edit) {
                return RespUtils.success(true);
            }
            return RespUtils.fail("修改用户信息失败！");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/user_info")
    public RespUtils<FullUserInfo> userInfo(HttpSession session, String user_id) {
        try {
            FullUserInfo user = usersService.getUserByUserId(session, user_id);
            if (user != null) {
                return RespUtils.success(user);
            }
            return RespUtils.fail("获取用户信息失败！");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }


    @GetMapping("/list")
    public RespUtils<ListUserResp> list(@RequestParam(defaultValue = "1", required = false) int page, @RequestParam(defaultValue = "10", required = false) int limit, String search, String dept_id) {
        try {
            PageInfo<FullUserInfo> usersPageInfo = usersService.list(page, limit, search, dept_id);
            if (usersPageInfo != null) {
                int total = (int) usersPageInfo.getTotal();
                int page_total = usersPageInfo.getPages();
                return RespUtils.success(new ListUserResp(usersPageInfo.getList(), total, page_total));
            }
            return RespUtils.fail("获取用户列表失败！");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @PostMapping("/login")
    public RespUtils<Boolean> login(HttpSession session, @RequestBody LoginUser user) {
        try {
            boolean isLogin = usersService.login(session, user.getWork_id(), user.getPassword());
            if (isLogin) {
                return RespUtils.success(true);
            }
            return RespUtils.fail("用户登录失败！");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    public RespUtils<Boolean> delete(HttpSession session, String user_id) {
        try {
            boolean isDel = usersService.delete(session, user_id);
            if (isDel) {
                return RespUtils.success(true);
            }
            return RespUtils.fail("删除用户失败！");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/all")
    public RespUtils<List<UserResp>> all() {
        try {
            return RespUtils.success(usersService.all());
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/check")
    public RespUtils<Boolean> check(HttpSession session){
        try{
            return RespUtils.success(usersService.check(session));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }

    @GetMapping("/check_list")
    public RespUtils<CheckListResp> checkList(HttpSession session, @RequestParam(defaultValue = "1", required = false) int page, @RequestParam(defaultValue = "10", required = false) int limit, String checkingDay){
        try{
            return RespUtils.success(usersService.checkList(session, page, limit, checkingDay));
        }catch (Exception e){
            logger.error(e.getMessage());
            return RespUtils.fail(e.getMessage());
        }
    }
}
